﻿using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.WebApi;
using GC_MU.Site.Modules.Autofac;

namespace GC_MU.Site
{
	public class MvcApplication : System.Web.HttpApplication
	{
		#region Variables

		private IContainer _diContainer;

		#endregion

		#region Methods

		protected void Application_Start()
		{
			AreaRegistration.RegisterAllAreas();
			GlobalConfiguration.Configure(WebApiConfig.Register);
			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			RouteConfig.RegisterRoutes(RouteTable.Routes);
			BundleConfig.RegisterBundles(BundleTable.Bundles);

			var builder = new ContainerBuilder();

			builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
			builder.RegisterModule(new SiteModule());

			_diContainer = builder.Build();

			GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(_diContainer);
		}

		protected void Application_Disposed()
		{
			if (_diContainer != null)
				_diContainer.Dispose();
		}

		#endregion
	}
}
